package com.hcmus.armobile;

import java.util.ArrayList;
import com.hcmus.armobile.R.style;
import common.StoreType;
import db.DBHandle;
import Server.HandleServer;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import global.*;
public class FavorCategories extends Activity {

	private ImageButton btnBackCategories;
	private LinearLayout wrapList;
	private ScrollView scroll;
	private Button btnSave;
	private ArrayList<StoreType>  array_categories = new ArrayList<StoreType>();
	private ArrayList<Integer> array_categories_favor_selected = new ArrayList<Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checklist_favorite_categories);
		
		wrapList = new LinearLayout(this);
		wrapList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		wrapList.setOrientation(LinearLayout.VERTICAL); 
		wrapList.setVisibility(View.VISIBLE);
		scroll = (ScrollView) findViewById(R.id.scrollListCategoriesFavor);
		// data test
		
		array_categories = DBHandle.GetListStoreType();
		
		for(StoreType item : array_categories ){
			//wrapList.addView(createItemCategories(item));
			LinearLayout layout = new LinearLayout(this);
			layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,48));
			layout.setOrientation(LinearLayout.HORIZONTAL);
			CheckBox cb = new CheckBox(this);
			cb.setTag(item);			
			int check = global.curUser.getType()&item.getId();
			if(check >0){
				array_categories_favor_selected.add(item.getId());				
				cb.setChecked(true);
			}
			
			cb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
			cb.setVisibility(View.VISIBLE);
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					StoreType categoryTag = (StoreType) buttonView.getTag();
					if(isChecked){
						// checked
						//array_categories_favor_selected.add(Integer.valueOf(buttonView.getTag().toString()));
						array_categories_favor_selected.add(Integer.valueOf(categoryTag.getId()));
					}else{
						// unchecked
						//array_categories_favor_selected.remove(Integer.valueOf(buttonView.getTag().toString()));
						array_categories_favor_selected.remove(Integer.valueOf(categoryTag.getId())); 
					}
				}
			});
			ImageView img = new ImageView(this);
			img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
			img.setVisibility(View.VISIBLE);
			TextView tv = new TextView(this);
			tv.setText(""+item.getName());
		    tv.setTextAppearance(this,style.text_label);
			tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setVisibility(View.VISIBLE);
			layout.addView(cb);
			layout.addView(img);
			layout.addView(tv);
			layout.setVisibility(View.VISIBLE);
			wrapList.addView(layout);
		}
		//wrapList.invalidate();
		scroll.addView(wrapList);
		// button back
		btnBackCategories = (ImageButton) findViewById(R.id.btnBackChecklistCategories);
		btnBackCategories.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(v.getContext(), Favorite.class);
				startActivity(in);
			}
		});
		// btn Save
		btnSave = (Button) findViewById(R.id.button_save_categories_favorite);
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (array_categories_favor_selected.size() == 0) {
					Toast.makeText(v.getContext(),
							"------------------Empty--------------------", 10)
							.show();
					return;
				}
				int total = 0;
				for (int item : array_categories_favor_selected) {
					total += item;
				}
				Toast.makeText(v.getContext(),
						"Total : " + String.valueOf(total), 10).show();
				
				/*congTT*/
				boolean result = HandleServer.UpdateType(total);
				if(result == true){
					Toast.makeText(v.getContext(),
							"Update Success !!! ", 10).show();
					
				}else{
					Toast.makeText(v.getContext(),
							"Update False !!! ", 10).show();
				}
				
				/**/
			}
		});
	}

}
